<?php

namespace App\V3\V3_2\Controllers;

use App\V3\V3_2\Models\AdminModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class SettingController extends BaseController
{
    public function setBatchTitle(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'batch_title' =>'required|max:4|alpha',//页面大小
        ],[
            'batch_title.alpha' => '`批次号标题`必须全为字母',
            'batch_title.required' => '`批次号标题`缺失',
            'batch_title.max' => '`批次号标题`超出最大4位长度',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        if($this->getMainUserId() != $this->getUserId()) {
            return $this->fail('', '权限不足，无法进行设置');
        }

        AdminModel::where('id', $this->getMainUserId())
            ->orWhere('parent_id', $this->getMainUserId())
            ->update(['batch_title' => $data['batch_title']]);

        $this->log('设置批次号标题', $data);
        return $this->success();
    }
}
